Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Guard for cookie deletion when name is empty #15074

Merged
merged 3 commits into from
Jan 13, 2025

Conversation

nvborisenko
Copy link
Member

@nvborisenko nvborisenko commented Jan 13, 2025

User description

Description

Empty cookie name should be invalid when deleting.

Motivation and Context

Contributes to #15044

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement, Bug fix


Description

  • Added validation for empty or null cookie names in DeleteCookieNamed and GetCookieNamed methods.

  • Updated exception type to ArgumentException for invalid cookie names.

  • Simplified dictionary initialization in DeleteCookieNamed method.

  • Improved code consistency and error handling in cookie management methods.


Changes walkthrough 📝

Relevant files
Enhancement
CookieJar.cs
Enhance cookie management with validation and exceptions 

dotnet/src/webdriver/CookieJar.cs

  • Added validation for empty or null cookie names.
  • Changed exception type to ArgumentException for invalid names.
  • Simplified dictionary initialization in DeleteCookieNamed.
  • Improved error handling in GetCookieNamed method.
  • +6/-5     
    Documentation
    ICookieJar.cs
    Update cookie interface documentation and validation         

    dotnet/src/webdriver/ICookieJar.cs

  • Updated exception type in DeleteCookieNamed documentation.
  • Added validation for empty or null cookie names.
  • +1/-1     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    15044 - Partially compliant

    Compliant requirements:

    • Add validation for cookie name parameter

    Non-compliant requirements:

    • Implement dedicated WebDriver Spec endpoint to get named cookie
    • Improve cookie handling by not fetching all cookies when only one is needed
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Incomplete Implementation

    The PR adds input validation but doesn't implement the main requirement of using the dedicated endpoint for getting named cookies. The code still fetches all cookies and filters them.

    public Cookie? GetCookieNamed(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
        {

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 13, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    Use more precise string validation to allow whitespace-containing cookie names

    Consider using string.IsNullOrEmpty() instead of IsNullOrWhiteSpace() for cookie
    name validation, as whitespace-only cookie names might be valid in some scenarios.

    dotnet/src/webdriver/CookieJar.cs [80-83]

    -if (string.IsNullOrWhiteSpace(name))
    +if (string.IsNullOrEmpty(name))
     {
    -    throw new ArgumentException("Cookie name cannot be empty", nameof(name));
    +    throw new ArgumentException("Cookie name cannot be null or empty", nameof(name));
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using IsNullOrEmpty() instead of IsNullOrWhiteSpace() is more appropriate for cookie name validation since whitespace-only names could be valid. This improves correctness of the validation logic.

    7
    ✅ Enhance error messages with actual values for better debugging experience
    Suggestion Impact:The commit modified the error message for empty cookie name, though in a different way than suggested - by clarifying that the name cannot be null or empty instead of showing the actual value

    code diff:

    -                throw new ArgumentException("Cookie name cannot be empty", nameof(name));
    +                throw new ArgumentException("Cookie name cannot be null or empty", nameof(name));

    Include the actual value in the exception message to help with debugging when an
    invalid cookie name is provided.

    dotnet/src/webdriver/CookieJar.cs [82]

    -throw new ArgumentException("Cookie name cannot be empty", nameof(name));
    +throw new ArgumentException($"Cookie name cannot be empty. Provided value: '{name}'", nameof(name));
    • Apply this suggestion
    Suggestion importance[1-10]: 4

    Why: Including the actual invalid value in the exception message would help with debugging, though the current message is already clear. This is a minor improvement to developer experience.

    4

    @nvborisenko
    Copy link
    Member Author

    It will be nice to add tests.

    @nvborisenko
    Copy link
    Member Author

    Added tests, ready to review.

    Copy link
    Contributor

    @RenderMichael RenderMichael left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Nice and simple, looks good!

    @nvborisenko nvborisenko merged commit bd225cc into SeleniumHQ:trunk Jan 13, 2025
    10 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants